package org.elte.lipika.client.managed.activity;

import com.google.gwt.activity.shared.Activity;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.requestfactory.shared.EntityProxyId;
import com.google.gwt.requestfactory.shared.RequestContext;
import org.elte.lipika.client.managed.request.ApplicationRequestFactory;
import org.elte.lipika.client.managed.request.SourceProxy;
import org.elte.lipika.client.managed.request.SourceRequest;
import org.elte.lipika.client.managed.ui.SourceDetailsView;
import org.elte.lipika.client.managed.ui.SourceEditView;
import org.elte.lipika.client.managed.ui.SourceListView;
import org.elte.lipika.client.managed.ui.SourceMobileDetailsView;
import org.elte.lipika.client.managed.ui.SourceMobileEditView;
import org.elte.lipika.client.scaffold.ScaffoldApp;
import org.elte.lipika.client.scaffold.place.CreateAndEditProxy;
import org.elte.lipika.client.scaffold.place.FindAndEditProxy;
import org.elte.lipika.client.scaffold.place.ProxyPlace;

public class SourceActivitiesMapper {

    private final ApplicationRequestFactory requests;

    private final PlaceController placeController;

    public SourceActivitiesMapper(ApplicationRequestFactory requests, PlaceController placeController) {
        this.requests = requests;
        this.placeController = placeController;
    }

    public Activity getActivity(ProxyPlace place) {
        switch(place.getOperation()) {
            case DETAILS:
                return new SourceDetailsActivity((EntityProxyId<SourceProxy>) place.getProxyId(), requests, placeController, ScaffoldApp.isMobile() ? SourceMobileDetailsView.instance() : SourceDetailsView.instance());
            case EDIT:
                return makeEditActivity(place);
            case CREATE:
                return makeCreateActivity();
        }
        throw new IllegalArgumentException("Unknown operation " + place.getOperation());
    }

    @SuppressWarnings("unchecked")
    private EntityProxyId<org.elte.lipika.client.managed.request.SourceProxy> coerceId(ProxyPlace place) {
        return (EntityProxyId<SourceProxy>) place.getProxyId();
    }

    private Activity makeCreateActivity() {
        SourceEditView.instance().setCreating(true);
        final SourceRequest request = requests.sourceRequest();
        Activity activity = new CreateAndEditProxy<SourceProxy>(SourceProxy.class, request, ScaffoldApp.isMobile() ? SourceMobileEditView.instance() : SourceEditView.instance(), placeController) {

            @Override
            protected RequestContext createSaveRequest(SourceProxy proxy) {
                request.persist().using(proxy);
                return request;
            }
        };
        return new SourceEditActivityWrapper(requests, ScaffoldApp.isMobile() ? SourceMobileEditView.instance() : SourceEditView.instance(), activity, null);
    }

    private Activity makeEditActivity(ProxyPlace place) {
        SourceEditView.instance().setCreating(false);
        EntityProxyId<SourceProxy> proxyId = coerceId(place);
        Activity activity = new FindAndEditProxy<SourceProxy>(proxyId, requests, ScaffoldApp.isMobile() ? SourceMobileEditView.instance() : SourceEditView.instance(), placeController) {

            @Override
            protected RequestContext createSaveRequest(SourceProxy proxy) {
                SourceRequest request = requests.sourceRequest();
                request.persist().using(proxy);
                return request;
            }
        };
        return new SourceEditActivityWrapper(requests, ScaffoldApp.isMobile() ? SourceMobileEditView.instance() : SourceEditView.instance(), activity, proxyId);
    }
}
